// axios可以统一做请求-响应拦截
// 设定请求超时
// 基于Promise
// 自动转换JSON数据
import axios from 'axios'
import qs from 'qs'
import Vue from 'vue'

// 新建axios实例
const Axios = axios.create({
    timeout: 30000 //设置超时
    // 还可以设置别的
})

export function get(url, params = {}) {
    Vue.$loading.show()
    return new Promise((resolve, reject) => {
        Axios({
            url,
            params,
            method: 'get'
        }).then(res => {
            resolve(res.data)
        }).catch(e => {
            reject(e)
        }).finally(() => {
            Vue.$loading.hide()
        })
    })
}
export function post(url, data = {}) {
    Vue.$loading.show()
    return new Promise((resolve, reject) => {
        data.t = Date.now()   //设置时间戳，解决缓存问题
        Axios({
            url,
            data: qs.stringify(data),
            method: 'post'
        }).then(res => {
            resolve(res.data)
        }).catch(e => {
            reject(e)
        }).finally(() => {
            Vue.$loading.hide()
        })
    })
}
// export function put(url, data = {}) {

// }
// export function del(url, params = {}) {

// }
// export { Axios }

// import * as $axios from '';
// Vue.prototype.$axios = $axios;

// this.$axios.get();